package mq

import (
	"github.com/streadway/amqp"
)

type RabbitMq struct {
	url  string `default:"amqp://guest:guest@localhost:5672/"`
	conn *amqp.Connection
}

func NewAmqp(url string) *RabbitMq {
	return &RabbitMq{url: url}
}

func (r *RabbitMq) Open() (err error) {
	if r.conn, err = amqp.Dial(r.url); err != nil {
		return
	}

	return nil
}

func (r *RabbitMq) GetChannel() (ch *amqp.Channel, err error) {
	if r.conn == nil {
		if r.conn, err = amqp.Dial(r.url); err != nil {
			return
		}
	}

	if ch, err = r.conn.Channel(); err != nil {
		return
	}
	return
}

func (r *RabbitMq) Close() {
	if r.conn != nil {
		r.conn.Close()
	}
}
